package com.examples.chartdirector.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Window;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

public class UIUtils
{
    public static void centerFrameOnScreen(final Window frame) {
        positionFrameOnScreen(frame, 0.5, 0.5);
    }

    public static void positionFrameOnScreen(final Window frame, final double horizontalPercent, final double verticalPercent) {
        final Rectangle s = frame.getGraphicsConfiguration().getBounds();
        final Dimension f = frame.getSize();
        final int w = Math.max(s.width - f.width, 0);
        final int h = Math.max(s.height - f.height, 0);
        final int x = (int)(horizontalPercent * w) + s.x;
        final int y = (int)(verticalPercent * h) + s.y;
        frame.setBounds(x, y, f.width, f.height);
    }
    
    public static void positionFrameRandomly(final Window frame) {
        positionFrameOnScreen(frame, Math.random(), Math.random());
    }
    
    public static void centerDialogInParent(final Dialog dialog) {
        positionDialogRelativeToParent(dialog, 0.5, 0.5);
    }
    
    public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent, final double verticalPercent) {
        final Container parent = dialog.getParent();
        if (parent == null) {
            centerFrameOnScreen(dialog);
            return;
        }
        final Dimension d = dialog.getSize();
        final Dimension p = parent.getSize();
        final int baseX = parent.getX();
        final int baseY = parent.getY();
        final int x = baseX + (int)(horizontalPercent * p.width);
        final int y = baseY + (int)(verticalPercent * p.height);
        final Rectangle s = parent.getGraphicsConfiguration().getBounds();
        final Rectangle r = new Rectangle(x, y, d.width, d.height);
        dialog.setBounds(r.intersection(s));
    }
    
    public static JPanel createTablePanel(final TableModel model) {
        final JPanel panel = new JPanel(new BorderLayout());
        final JTable table = new JTable(model);
        for (int columnIndex = 0; columnIndex < model.getColumnCount(); ++columnIndex) {
            final TableColumn column = table.getColumnModel().getColumn(columnIndex);
            final Class<?> c = model.getColumnClass(columnIndex);
            if (c.equals(Number.class)) {
                column.setCellRenderer(new NumberCellRenderer());
            }
        }
        panel.add(new JScrollPane(table));
        return panel;
    }
    
    public static JLabel createJLabel(final String text, final Font font) {
        final JLabel result = new JLabel(text);
        result.setFont(font);
        return result;
    }
    
    public static JLabel createJLabel(final String text, final Font font, final Color color) {
        final JLabel result = new JLabel(text);
        result.setFont(font);
        result.setForeground(color);
        return result;
    }
    
    public static JButton createJButton(final String label, final Font font) {
        final JButton result = new JButton(label);
        result.setFont(font);
        return result;
    }
}

